<?PHP
//line 788
/**
* @company Libertas Solutions Ltd
* @package com.solutions.libertas.cms
* @author Adrian Sweeney
* @file libertas.events.php
* @created 10 Dec 2004
*/
/**
* This module is the module for managing Editor functionality access for users. it allows you to
* manage a selection of editor configurations and to define that a group has access to a specific editor
*
* it also allows you to specify the default editor for a modules form.
*/
require_once ("libertas.information_presentation.php");
class events extends information_presentation{
	/**#@+
	* Class Variables
    * @var string
	*/
	var $module_grouping			= "LOCALE_MANAGEMENT_GROUP_CONTENT";
	var $module_name				= "events";
	var $module_presentation_name	= "events";
	var $module_name_label			= "Event Management Module (Presentation)";
	var $module_admin				= "0";
	var $module_channels			= "";
	var $module_modify	 			= '$Date: 2005/02/08 17:01:10 $';
	var $module_version 			= '$Revision: 1.13 $';
	var $module_command				= "EVENT_"; 		// all commands specifically for this module will start with this token
	var $webContainer				= "EVENT_";
	var $webAdmin					= "EVENTADMIN_";
	var $module_label				= "MANAGEMENT_EVENT";
	var $shop_type					= "EVENT";
	var $cal_visible				= false;
	
	/*************************************************************************************************************************
    * extra commands defined by
    *************************************************************************************************************************/
	var $extra_commands = Array(
		Array("EVENT_SHOW_CALENDAR", "show_calendar"),
		Array("EVENT_SHOWLIST", "showlist"),
	);
	/*************************************************************************************************************************
	* @function Initialise function
	*
	* This function will initialise some variables for this modules functions to use.
	*
	* this function is called by the constructor it over writes the basic
	* module::initialise() function allowing you to define any extra constructor
	* functionality.
    *************************************************************************************************************************/
	function initialise(){
		if ($this->module_debug){
			$this->call_command("UTILS_DEBUG_ENTRY",array($this->module_name,"initialise()",__LINE__,""));
		}
		/**
		*request the client identifier once we use this variable often
		*/
		$this->client_identifier = $this->parent->client_identifier;
		parent::initialise();
		return 1;
	}
	
	/*************************************************************************************************************************
    * show_calendar
    *************************************************************************************************************************/
	function show_calendar($parameters){
		if($this->cal_visible) return "";
		$this->cal_visible = true;
		$today	= split("-",$this->libertasGetDate("Y-m-d"));
		$list	= $this->check_parameters($parameters,"wo_owner_id",$this->check_parameters($parameters,"identifier",-1));
		$y		= $this->check_parameters($parameters,"year", $this->check_parameters($parameters,"y", date("Y")));
		$m		= $this->check_parameters($parameters,"month", $this->check_parameters($parameters,"m", date("m")));
		$d		= $this->check_parameters($parameters,"d", -1);
		$ym		= $this->check_parameters($parameters,"ym", "");
		if($ym!=""){
			$l = split("::",$ym);
			$y = $l[0];
			$m = $l[1];
		}
		$params = split("::",date("l::t::D",  mktime (0,0,0,$m,1,$y)));
		$maxendyear = "";
		$maxendmonth = "";
		$minstartyear = "";
		$minstartmonth = "";
		$sql = "
			select 
				ie_parent,
				md_identifier, 
				DATE_FORMAT(md_date_available,'%Y-%m-%d') as eventStart,
				DATE_FORMAT(md_date_remove,'%Y-%m-%d') as eventEnds,
				menu_url, 
				wo_show_label , 
				wo_label, ie_uri
			from information_entry 
				inner join metadata_details on md_link_id = ie_identifier and md_module='EVENT_' and md_client= ie_client 
				inner join information_list on info_identifier = ie_list and ie_client = info_client 
				inner join menu_data on info_menu_location = menu_identifier and menu_client = info_client 
				inner join web_objects on wo_client=ie_client and wo_owner_module='EVENT_SHOW_CALENDAR' 
			where 
				ie_published=1 and ie_status = 1 and ie_client=4 
				and (
						(PERIOD_DIFF( DATE_FORMAT('2009-01-01','%Y%m'), DATE_FORMAT(md_date_available, '%Y%m'))=0 and PERIOD_DIFF( DATE_FORMAT('2009-01-31','%Y%m'), DATE_FORMAT(md_date_remove, '%Y%m')) =0)
					or 
						(PERIOD_DIFF( DATE_FORMAT('2009-01-01','%Y%m'), DATE_FORMAT(md_date_available, '%Y%m'))>0 and PERIOD_DIFF( DATE_FORMAT('2009-01-31','%Y%m'), DATE_FORMAT(md_date_remove, '%Y%m')) =0)
					or 
						(PERIOD_DIFF( DATE_FORMAT('2009-01-01','%Y%m'), DATE_FORMAT(md_date_available, '%Y%m'))>0 and PERIOD_DIFF( DATE_FORMAT('2009-01-31','%Y%m'), DATE_FORMAT(md_date_remove, '%Y%m')) <0)
					or 
						(PERIOD_DIFF( DATE_FORMAT('2009-01-01','%Y%m'), DATE_FORMAT(md_date_available, '%Y%m'))=0 and PERIOD_DIFF( DATE_FORMAT('2009-02-01','%Y%m'), DATE_FORMAT(md_date_remove, '%Y%m')) <0)
					)
				group by md_date_available, md_date_remove 
				order by md_date_available, md_date_remove, ie_uri
				";
//and PERIOD_DIFF(DATE_FORMAT(current_timestamp,'%Y%m'), DATE_FORMAT(md_date_remove,'%Y%m'))<=0
//and PERIOD_DIFF(DATE_FORMAT(current_timestamp,'%Y%m'), DATE_FORMAT('$y-$m-01 00:00:00','%Y%m')) = 0


		//print "<pre>$sql";
		$result	= $this->db->query($sql);
		$listOfDates = Array();
		$earliest = $r["eventStart"];
		$menu_url = "";
		while($r = $this->db->fetch_array($result)){
			if($earliest > $r["eventStart"]){
				$earliest = $r["eventStart"];
			}
			$today = $r["eventStart"];
			$menu_url = dirname($r["menu_url"]);
			$listOfDates[$today]="0";
			while($today!=$r["eventEnds"]){
				$listOfDates[$today]="0";
				$today = date("Y-m-d",strtotime($today." +1 day"));
			}
			$listOfDates[$r["eventEnds"]]="0";
		}	
		//print_r($listOfDates);
		$this->db->free_result($result);
		$listOfDates = array_keys($listOfDates);
		asort($listOfDates);
		//print_R($listOfDates);
		$mths = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
		$daysOfTheWeek = Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
		$output = "<table summary='calendar of events one month visual' class='calendar'>
					<tr>
						<td colspan='7' class='centeralign'>
							<a title='Previous month' href='[[script]]?year=".date("Y",strtotime("$y-$m-01 -1 month"))."&month=".date("m",strtotime("$y-$m-01 -1 month"))."'>[[leftarrow]][[leftarrow]]</a>
							[[nbsp]] ".$mths[$m-1]." $y [[nbsp]]
							<a title='Next month' href='[[script]]?year=".date("Y",strtotime("$y-$m-01 +1 month"))."&month=".date("m",strtotime("$y-$m-01 +1 month"))."'>[[rightarrow]][[rightarrow]]</a>
						</td>
					</tr>";
		$startDay = date("D",strtotime("$y-$m-01"));
		$endDay = date("t",strtotime("$y-$m-01"));
		$monthStart = false;
		$day = 1;
		$output.="<tr>";
		for($i=0;$i<7;$i++){
			$output.="<th>$daysOfTheWeek[$i]</th>";
		}
		$output.="</tr>";
		for($z=0;$z<5;$z++){
			$output.="<tr>";
			for($i=0;$i<7;$i++){
				if(!$monthStart && $startDay==$daysOfTheWeek[$i]){
					$monthStart = true;
				}
				if($monthStart && $day>$endDay){
					$monthStart = false;
				}
				if(!$monthStart){
					$output.="<td>-</td>";
				} else {
					$today = substr("0$day",-2);
					if(in_array("$y-$m-$today",$listOfDates)){
						$this->makeSpecial("EVENT_SHOWLIST","$menu_url/index.php", $this->check_parameters($this->parent->site_directories,"ROOT")."/".$menu_url."/-event-$y.php",                  Array("year"=> $y),"Events in $y");
						$this->makeSpecial("EVENT_SHOWLIST","$menu_url/index.php", $this->check_parameters($this->parent->site_directories,"ROOT")."/".$menu_url."/-event-$y-$m.php",         Array("year"=> $y ,"month"=>$m),"Events in $y-$m");
						$this->makeSpecial("EVENT_SHOWLIST","$menu_url/index.php", $this->check_parameters($this->parent->site_directories,"ROOT")."/".$menu_url."/-event-$y-$m-$today.php", Array("year"=> $y ,"month"=>$m, "day"=>$today),"Events in $y-$m-$today");
						$output.="<td><a href='".$menu_url."/-event-$y-$m-$today.php'>$today</a></td>";
					} else {
						$output.="<td>$today</td>";
					}
					$day++;
				}
			}
			$output.="</tr>";
		}
		$output.= "</table>";

		if($show_label==0){
			return "<module name='$this->module_name' display='TEXT'><text><![CDATA[$output]]></text></module>";
		} else {
			return "<module name='$this->module_name' display='TEXT'><label show='1'><![CDATA[$label]]></label><text><![CDATA[$output]]></text></module>";
		}
	}
	/** function to show list of events that are on, on a sopecific day.
	  * 
	  **/
	function showlist($p){
//	print_R($p);
		$y		= $this->check_parameters($p,"year", -1);
		$m		= $this->check_parameters($p,"month", -1);
		$d		= $this->check_parameters($p,"day",   -1);
		
		if ($y ==-1) return ""; // fail
		if($m!=-1){
			if($d==-1){
				$dateCondition = "and (md_date_available <= cast('$y-$m' as Date)  and  md_date_remove>=cast('$y-$m' as Date))";
			} else {
				$dateCondition = "and (md_date_available <= cast('$y-$m-$d' as Date)  and  md_date_remove>=cast('$y-$m-$d' as Date))";
			}
		} else {
			$dateCondition = "and(Year(md_date_available) = '$y' or Year(md_date_remove) = '$y')";
		}
		
		$SelectSQL = "
			select 
				ie_identifier
				from information_entry
				inner join metadata_details on md_link_id = ie_identifier and md_module='EVENT_' and md_client= ie_client
				inner join information_list on info_identifier = ie_list and ie_client = info_client 
				inner join menu_data on info_menu_location = menu_identifier and menu_client = info_client 
				inner join web_objects on wo_client=ie_client 
				where 
				ie_published=1 and ie_status = 1 and ie_client=$this->client_identifier 
				$dateCondition
				group by md_date_available, md_date_remove 
				order by md_date_available, md_date_remove, ie_uri
				";
		/*--and wo_owner_module='EVENT_SHOW_LIST'*/
		//print "<li>$SelectSQL";
		
		$menu_url = "";
		$data_files = $this->parent->site_directories["DATA_FILES_DIR"];
		$output="";
		$list = Array();

		$resultData = $this->db->query($SelectSQL);
		while($r = $this->db->fetch_array($resultData)){
			$list[] = $r["ie_identifier"];
		}
		$this->db->free_result($resultData);

		if($list !=""){	
			$sql = "select distinct
	ie_identifier,ie_list, md_title, md_description, md_date_available, md_date_remove,ie_uri, info.info_menu_location
	,cto.cto_clist, menu_data.menu_url, cat.cat_list_id
from information_entry as ie 
	inner join information_entry_values as iev on ie_identifier = iev_entry 
	inner join information_fields on if_name = iev_field and if_list = ie_list 
	inner join metadata_details on md_identifier = ie_parent
	inner join information_list as info on info_identifier= ie_list
	inner join category_to_object as cto on cto_object = ie_identifier
	inner join category as cat on cto_clist = cat_identifier
	inner join menu_data  on  menu_identifier = info_menu_location
where 
	ie_identifier in(".implode(",",$list).") and 
	if_screen=0 and ie_published=1 and ie_status = 1 
	and ie_client=$this->client_identifier";
			$resultData = $this->db->query($sql);
			//print "<li>$sql";
			$list ="";
			while($r = $this->db->fetch_array($resultData)){
				$category = $this->call_command("CATEGORY_GET_BREADCRUMBTRAILS",
					Array(
						"object" => $r["ie_identifier"],
						"list"=>$r["ie_list"],
						"category_list"=>$r["cat_list_id"],
						"module"=>"EVENT_",
						"split_categories"=>"/"
					)
				);
				//print "[".print_R($category,true)."]";
				$path = "/";
				foreach($category as $cat){
					$path .= $this->make_uri($cat)."/";
				}
				$list .= "<p class='event'><a class='title' href='".dirname($r["menu_url"])."$path". $r["ie_uri"]."'>".$r["md_title"]."</a><br /><span class='description'>".$r["md_description"]."<br />From ".date("d M Y", strtotime($r["md_date_available"]))." to ".date("d M Y", strtotime($r["md_date_remove"]))."</p>";
			}
			$this->db->free_result($resultData);
			return "<module name='$this->module_name' display='TEXT'><text><![CDATA[<div class='eventlist'><h1>List of events on ".Date("d M Y",strtotime("$y-$m-$d"))."</h1>$list</div>]]></text></module>";
		} else {
			return "<module name='$this->module_name' display='TEXT'><text><![CDATA[<div class='eventlist'><h1>List of events on ".Date("d M Y",strtotime("$y-$m-$d"))."</h1><p>No Events Found</p></div>]]></text></module>";
		}
	}

}
?>